<?php

namespace Respect\Http;

use Respect\Environment\EnvironmentableInterface;

class Request
{

    protected $environment = null;
    protected $modules = array();
    protected $response;

    public function __construct(EnvironmentableInterface $environment)
    {
        $this->environment = $environment;
    }

    public function getModule($name)
    {
        if (!isset($this->modules[$name])) {
            $className = __NAMESPACE__ . '\\Module\\' . $name;
            $this->modules[$name] = call_user_func(
                    array($className, 'qualify'),
                    $this->environment
            );
        }
        return $this->modules[$name];
    }

    public function getModuleLoader()
    {
        $request = $this;
        return function ($moduleName) use ($request) {
            return $request->getModule($moduleName);
        };
    }

    public function getEnvironment()
    {
        return $this->environment;
    }

}
